const db = cloudbaseApp.database()
const ZMNMEIDB = 'zanmeidb-new'
const VERSIONDB = 'sgb-version-control'
//服务器请求基础路径
const BASEHOST =  'https://7367-sgb-8gqmg7kv48722f4e-1306391227.tcb.qcloud.la'

//花朵音视频请求url服务器
// const HDMADIAURI = BASEHOST + '/hd/hddmt/'
// const HDMADIAURI = ''
//赞美音频请求URL服务器
const ZMMADIAURI = BASEHOST + '/sgb/ad/'

//获取全部赞美列表
function getZmItem(){
    const zmct = db.collection(ZMNMEIDB)
    return zmct.limit(1000)
            .where({isupper: true})
            .orderBy('shijiIndex','asc')
            .orderBy('xuhao','desc')
            .get()
}

// 调用数据库版本控制的api
async function getSgbVersion(){
    let res = await db.collection(VERSIONDB).orderBy('update','desc').limit(1).get()
    return res.data[0]
}
//获取指定ID的赞美列表
async function getZmItemWhereId(ids){
    const zmct = db.collection(ZMNMEIDB)
    const _ = db.command;
    const localZanmeiItem = localStorage.getItem('zmitem')
    let res
    if(localZanmeiItem){
        const zanmeiItem = JSON.parse(localZanmeiItem)
        res={}
        res['data'] = zanmeiItem.filter(item=>ids.includes(item._id))
    }else{
        res =  await zmct.limit(1000).where({_id:_.in(ids)}).get()
    }
    return res
}

// 调用数据库版本列表的api
async function getSgbVersionList(){
    let res = await db.collection(VERSIONDB).orderBy('update','desc').limit(1000).get()
    return res.data
}
//根据诗集类型获取赞美
async function getZmWhereType(shiJiIndex){
    let res
    let items = localStorage.getItem('zmitem')
    if(items){
        let arrs = JSON.parse(items)
        let r = arrs.filter(item=>{
            return item.shijiIndex == shiJiIndex
        })
        res = {data:r}
    }else{
        const zmct = db.collection(ZMNMEIDB)
        res = await zmct.limit(1000)
            .where({'shijiIndex':shiJiIndex})
            .orderBy('xuhao','desc')
            .get()
    }
    console.log('res=>',res)
    return res.data
}


//花朵数据Api接口
const HDDB = { //花园花朵云函数

    //获取花朵的列表
    getHdList:async function (fun,page = 1,limit = 5,dateWher = 20220101,dateWherStar = dateFormat(new Date(),'yyyyMMdd')){
        const _ = db.command
        const list = await db.collection('flower-garden-list')
             .where({
                 _id:_.and(_.gte(dateWher),_.lte(parseInt(dateWherStar)))
             })
             .limit(limit)
             .skip((page-1)*limit)
            .orderBy('_id','desc')
            .get()

        // const count =  await db.collection('flower-garden-list')
        //                 .where({
        //                     _id:_.and(_.gte(dateWher),_.lte(parseInt(dateWherStar)))
        //                 }).count()

        let tmp = { result: { list: { data: list.data }, pages: 1 } } //  let tmp = { result: { list: { data: list.data }, pages: 1 } } //

        fun(tmp)
    },
    //获取花朵的视频列表
    getHdVideolist:async function(page,limit,fun){

       const list = await db.collection('flower-garden-video-list').limit(limit).skip((page-1)*limit)
            .orderBy('_id','desc').get()
        const count = await db.collection('flower-garden-video-list').count()
        const pages = Math.ceil(count.total /limit )
        fun({result:{list:list,pages}})

    },
    //获取花朵的每一个详情的列表
    gethditem:async function(id,fun){
        const item = await db.collection('flower-garden-list').where({_id:id}).get()
        const data = item.data[0]
        const jw = await db.collection('jingWenDb').where({_id:data.jw_Id}).get()
        const zm = await db.collection(ZMNMEIDB).where({_id:data.zm_Id}).get()
        item.data[0]['jw'] = jw.data
        item.data[0]['zm'] = zm.data
        fun({result:{list:item.data}})
    }
}
//话语调取接口
const HYDB = {
    gitZRList : async function (){
        let res = await db.collection('huayu-db-new').where({type:'主日信息'}).limit(1000).orderBy('sort','desc').get()
        return res.data
    }
}

